
// 识别大小
export function getMinMax(num1: number, num2: number) {
  const min = Math.min(num1, num2);
  const max = Math.max(num1, num2);
  return { min, max };
}

// 生成随机整数
export function getRandomNumber(_min: number, _max: number) {
  if (_min === _max) {
    return _min;
  }
  const { min, max } = getMinMax(_min, _max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 格式化一个持续时间，单位秒，比如 333 ->
export function formatDuration(duration: number) {
  const h = Math.floor(duration / (60 * 60));
  duration = duration - h * 60 * 60;
  const m = Math.floor(duration / 60);
  const s = duration - m * 60;
  function fn(val: number | string, length = 2) {
    return (val + '').padStart(length, '0');
  }
  return `${fn(h)}:${fn(m)}:${fn(s)}`;
}


export function sleep(timeout: number): Promise<number> {
  return new Promise((res) => {
    setTimeout(() => {
      res(timeout);
    }, timeout);
  });
}

